package com.viu.technology.controller;

import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsResponse;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.viu.technology.dto.Result;
import com.viu.technology.dto.ResultCode;
import com.viu.technology.service.sms.SmsService;
import com.viu.technology.util.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.security.Security;

@RestController
@RequestMapping("/api/validate")
public class ValidateController {

    @Autowired
    private SmsService smsService;


    @GetMapping("/sendValidateCode")
    public Result sendValidateCode(@RequestParam(required = true) String phone) {
        SendSmsResponse response = smsService.sendValidateCode(phone);
        if (null == response) {
            return Result.fail(ResultCode.SMS_SEND_FAIL);
        } else {
            return Result.success(response);
        }
    }

    @GetMapping("/checkValidateCode")
    public Result checkValidateCode(@RequestParam(required = true) String phone, @RequestParam(required = true) String code) {
        if (smsService.checkValidateCode(phone, code)) {
            return Result.fail(ResultCode.SMS_CODE_VALID_FAIL);
        } else {
            return Result.success("校验成功");
        }
    }

    @GetMapping("/queryValidateCodeDetails")
    public Result queryValidateCodeDetails(@RequestParam(required = true) String bizId) {
        QuerySendDetailsResponse response=smsService.querySendDetails(bizId);
        if (null!=response) {
            return Result.fail(ResultCode.SMS_QUERY_NOT_FOUND);
        } else {
            return Result.success(response);
        }
    }
}
